﻿#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QDir>
#include <QDebug>
#include <QFile>
#include <QTextStream>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_findBtn_clicked()
{
    QString strName = QFileDialog::getExistingDirectory(this, tr("Find"), "F:/deploy");
    QStringList listDir;
    if(strName.isEmpty())
    {
        qWarning() << "getExistingDirectory is NULL!";
    }

    QFile outFile("out1.txt");
    if(!outFile.open(QIODevice::WriteOnly | QIODevice::Append))
    {
        qWarning() << "outFile error";
        return;
    }
    QTextStream out(&outFile);
    listDir = getDirPathOfSplAndChildDir(strName);
    for(int i =0; i < listDir.size(); i++)
    {
        qWarning() << "listDir:" << listDir[i];
        out << listDir[i] << "\r\n";
    }
    qWarning() << "export1 success";
}

QStringList Widget::getDirPathOfSplDir(QString dirPath)
{
    QStringList dirPaths;
    QDir splDir(dirPath);
    QFileInfoList fileInfoListInSplDir = splDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
    QFileInfo tempFileInfo;
    foreach (tempFileInfo, fileInfoListInSplDir) {
        dirPaths << tempFileInfo.absoluteFilePath();
    }
    return dirPaths;
}

QStringList Widget::getDirPathOfSplAndChildDir(QString dirPath)
{
    QStringList dirPathNames;
    dirPathNames << getDirPathOfSplDir(dirPath);

    QString tempChildDir;
    foreach (tempChildDir, dirPathNames) {
        dirPathNames << getDirPathOfSplAndChildDir(tempChildDir);
    }
    return dirPathNames;
}

void Widget::on_find2Btn_clicked()
{
    QStringList listName;
    QString strName = QFileDialog::getExistingDirectory(this, tr("Find"), "F:/deploy");
    if(strName.isEmpty())
    {
        qWarning() << "getExistingDirectory2 is NULL!";
    }

    QFile outFile("out2.txt");
    if(!outFile.open(QIODevice::WriteOnly | QIODevice::Append))
    {
        qWarning() << "outFile error";
        return;
    }
    QTextStream out(&outFile);
    listName = getExitDir(strName);
    for(int i =0; i < listName.size(); i++)
    {
//        qWarning() << "listName:" << listName[i];
        out << listName[i] << "\r\n";
    }
    qWarning() << "export2 success";
}

QStringList Widget::getExitDir(QString strName)
{
    QStringList listDir;
    QStringList listName;

    QDir dir(strName);
    strName = dir.fromNativeSeparators(strName);//  "\\"转为"/"
    if(!dir.exists())
    {
        listDir = QStringList();
    }

    dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
    listDir = dir.entryList();

    for(int i =0; i < listDir.size(); i++)
    {
        qWarning() << "listDir:" << strName + "/" + listDir[i];
        listName << (strName + "/" + listDir[i]);
        listName << getExitDir(strName + "/" + listDir[i]);
    }
    return listName;
}
